<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:29
 */

class LevelService extends Service {

    public static $levelKeys = array(
        '1' =>  '0 ~ 10',
        '2' =>  '11 ~ 20',
        '3' =>  '21 ~ 30',
        '4' =>  '31 ~ 40',
        '5' =>  '41 ~ 50',
        '6' =>  '51 ~ 60',
        '7' =>  '61 ~ 70',
        '8' =>  '71 ~ 80',
        '9' =>  '81 ~ 90',
        '10' => '91 ~ 100',
        '11' => '1080 ~ 1090',
        '12' => '1091 ~ 1100',
        '13' => '1101 ~ 1110',
        '14' => '1111 ~ 1120',
        '15' => '2100 ~ 2110',
        '16' => '2111 ~ 2120',
        '17' => '2121 ~ 2130',
        '18' => '2131 ~ 2140',
        '19' => '3120 ~ 3130',
        '20' => '3131 ~ 3140',
        '21' => '3141 ~ 3150',
        '22' => '3151 ~ 3160',
        '23' => '4140 ~ 4150',
        '24' => '4151 ~ 4160',
        '25' => '4161 ~ 4170',
        '26' => '4171 ~ 4180',
        '27' => '5160 ~ 5170',
        '28' => '5171 ~ 5180',
        '29' => '5181 ~ 5190',
        '30' => '5191 ~ 5200',
//        '11' =>  '101 ~ 110',
//        '12' =>  '111 ~ 120',
//        '13' =>  '121 ~ 130',
//        '14' =>  '131 ~ 140',
//        '15' =>  '1001 ~ 1140',
//        '16' =>  '2001 ~ 2140',
//        '17' =>  '3001 ~ 3140',
//        '18' =>  '4001 ~ 4140',
//        '19' =>  '5001 ~ 5140',
    );

    public static function getDistributionDict(){
        $distributionDict = array(
            'AllUser'   => PubFunc::t('所有玩家'),
            'NewUserLost'  => PubFunc::t('新用户流失'),
            'PayUser'  => PubFunc::t('充值玩家'),
        );
        return $distributionDict;
    }
    public static function indexHeader(){
        $header = array(
            'RecDate'       => array('text' => PubFunc::t('日期')),
            'SID'           => array('text' => PubFunc::t('服')),
            'RoleUser'      => array('text' => PubFunc::t('累计用户数'),'rule'=>'numeric'),
//            'LiveUser'      => array('text' => PubFunc::t('活跃用户数'),'rule'=>'numeric'),
        );

        foreach(self::$levelKeys as $k => $v){
            $header['L'.$k] = array('text' => $v,'rule'=>'numeric');
        }

        return $header;
    }


    public static function detailsHeader(){
        $header = array(
            'RecDate'       => array('text' => '日期'),
            'SID'           => array('text' => '服'),
            'Level'         => array('text' => '用户等级','rule'=>'numeric'),
            'sLevelUsers'   => array('text' => '初始等级人数','rule'=>'numeric'),
            'sTime'         => array('text' => '升级时间(分钟)','rule'=>'numeric'),
            'sUser'         => array('text' => '当日最终等级人数','rule'=>'numeric'),
            'sUser_Lost'    => array('text' => '滞留人数(无升级人数)','rule'=>'numeric'),
        );

        return $header;
    }

    public static function detailsData($params){
        $data = ModLevel::detailsData($params);

        return $data;
    }

    public static function roleHeader($params){
        $header = array(
            'RoleOcc'       => array('text' => '职业ID'),
            'RoleName'      => array('text' => '职业名称')
        );

        foreach(self::getLevel($params['min_level'], $params['max_level']) as $v){
            $header['L'.$v] = array('text' => $v,'rule'=>'numeric', 'unSort' => 1);
        }

        return $header;
    }

    public static function roleData($params){
        $tmp = ModLevel::roleData($params);

        $result = array();
        $level = self::getLevel($params['min_level'], $params['max_level']);

        $roleList = PubDict::dict('roma', 'roleocc');

        foreach($tmp as $v){
            if(!isset($result['R'.$v['RoleOcc']])){
                $tmp = array(
                    'RoleOcc'   => $v['RoleOcc'],
                    'RoleName'  => $roleList[$v['RoleOcc']]
                );

                foreach($level as $lv){
                    $tmp['L'.$lv] = 0;
                }

                $result['R'.$v['RoleOcc']] = $tmp;
            }

            $result['R'.$v['RoleOcc']]['L'.$v['Level']] = $v['sUser'];
        }

        return $result;
    }

    public static function roleChart($data, $params){
        $chartData = array();

        $level = self::getLevel($params['min_level'], $params['max_level']);

        foreach($data as $v){
            $key = empty($v['RoleName']) ? $v['RoleOcc'] : $v['RoleName'];

            foreach($level as $lv){
                $chartData[$key][$lv] = $v['L'.$lv];
            }
        }
        return $chartData;
    }

    public static function detailsChart($data){
        $chartData = array();

        foreach($data as $v){
            $chartData['等级人数'][$v['Level']] = $v['sUser'];
        }

        return $chartData;
    }

    public static function getLevel($minLevel, $maxLevel){
        if(empty($minLevel)){
            $minLevel = 1;
        }
        if(empty($maxLevel)){
            $maxLevel = 100;
        }

        $level = array();
        for($i=$minLevel; $i<=$maxLevel; $i++){
            $level[] = $i;
        }
        return $level;
    }
    public static function independentHeader($params){
        $header = array(
//            'RecDate'   => array('text' => PubFunc::t('日期')),
//            'SID'   => array('text' => PubFunc::t('服')),
            'RoleLevel'      => array('text' => PubFunc::t('等级'),'rule'=>'numeric'),
//            'RoleUser'      => array('text' => PubFunc::t('累计用户数'),'rule'=>'numeric'),
//            'RoleUserRate'      => array('text' => PubFunc::t('占比'),'rule'=>'numeric'),
        );
        $min = strtotime(PubStat::min($params['time_start'],$params['time_end']));
        $max = strtotime(PubStat::max($params['time_start'],$params['time_end']));
        for($min;$min <= $max;$min +=86400){
//            $header[date("Y-m-d",$min)] = array('text' => date("Y-m-d",$min),'rule'=>'numeric');
            $header[date("Y-m-d",$min) . "_Rate"] = array('text' => date("Y-m-d",$min),'rule'=>'numeric');
        }
        return $header;
    }

    public static function indexData($params){
        $data = ModLevel::indexData($params);

        return $data;
    }

    public static function indexChart($data){
        if(empty($data)){
            return array();
        }
        $chartData = array();
        $maxDate = '1999-01-01';
        foreach($data as $v){
            $maxDate = PubStat::max($maxDate,$v['RecDate']);
            foreach(self::$levelKeys as $k => $vv){
                $chartData[$v['RecDate']][PubFunc::t($vv)] = $v["L".$k];
            }
        }

        $data = array();
        $maxTime = strtotime($maxDate);
        for($i=2; $i>0; $i--){
            $oneDate = date("Y-m-d", $maxTime-$i*86400);
            if(isset($chartData[$oneDate])){
                $data[$oneDate] = $chartData[$oneDate];
            }
        }
        $data[$maxDate] = $chartData[$maxDate];

        return array(
            "data"=> $data
        );
    }

    public static function independentData($params){
        $data = ModLevel::independentData($params);

        $returnData = array();
        foreach($data as $item){
            if(!isset($returnData["Level" . $item['RoleLevel']])){
                $returnData["Level" . $item['RoleLevel']] = array("RoleLevel" => $item['RoleLevel']);
            }
            $returnData["Level" . $item['RoleLevel']][$item['RecDate']] = $item["RoleUser"];
//            echo $item['RecDate'] . "<br>";
        }
//        LibUtil::var_dump($returnData);die;
        $min = strtotime(PubStat::min($params['time_start'],$params['time_end']));
        $max = strtotime(PubStat::max($params['time_start'],$params['time_end']));
        for($min;$min <= $max;$min +=86400){

            $total = array();
            $date = date("Y-m-d",$min);
            $allParams = array(
                "total" => array("Level"=>"0"),
                "sumKey"=>array($date)
            );
            $rateConfig = array(
                $date   =>  array("to" =>  $date . "_Rate",$allParams),
            );
            PubStat::setRate($returnData,$rateConfig,$allParams,$total,true);
        }

        return $returnData;
    }

    public static function independentChart($data,$params){
        $chartData = array();
        if(empty($data)){
            return $chartData;
        }
        foreach($data as $v){
            $min = strtotime(PubStat::min($params['time_start'],$params['time_end']));
            $max = strtotime(PubStat::max($params['time_start'],$params['time_end']));
            for($min;$min <= $max;$min +=86400){
                $k = date('Y-m-d',$min);
                for($i = 1;$i <= 100;$i++){
                    if(!isset($chartData[$k][$i])){
                        $chartData[$k][$i] = 0;
                    }
                }
                $chartData[$k][$v["RoleLevel"]] = $v[$k];
            }
        }

        return array(
            "data"=> $chartData
        );
    }



    public static function distributionHeader($params){
        $header = array(
            'RecDate'   => array('text' => PubFunc::t('日期'),"width" => "10%"),
//            'SID'   => array('text' => PubFunc::t('服')),
            'RoleLevel'      => array('text' => PubFunc::t('等级'),'rule'=>'numeric',"width" => "10%"),
//            'KPI'      => array('text' => PubFunc::t('KPI'),'rule'=>'numeric'),
            'RoleUser'      => array('text' => PubFunc::t('用户数'),'rule'=>'numeric',"width" => "10%"),
            'RoleUserRate'      => array('text' => PubFunc::t('所占比例'),'rule'=>'numeric'),
        );
        return $header;
    }

    public static function distributeData($params){
        $data = ModLevel::distributeData($params);
        $allParamsGet = array(
            "total" => array("RecDate"=>"Total"),
            "sumKey"=>array("RoleUser")
        );
        $getRateConfig = array(
            "RoleUser"   =>  array("to" =>  "RoleUserRate",$allParamsGet),
        );
        PubStat::setRate($data,$getRateConfig,$allParamsGet,$getTotal);
        PubFunc::parseDict($data,array("KPI"=>self::getDistributionDict()));
        return $data;
    }

    public static function distributeChartData($data,$params){
        $kpi = $params['kpi'];
        $labels = array(
            "NewUserLost" => PubFunc::t("流失角色数-")."#day#".PubFunc::t("日"),
            "AllUser" 	  => PubFunc::t("角色数-")."#day#".PubFunc::t("日"),
            "PayUser" 	  => PubFunc::t("角色数-")."#day#".PubFunc::t("日"),
        );
        $chartData = array();
        $label = $labels[$kpi];
        foreach($data as $item){
            for($i = 0;$i < 8;$i++){
                if($i === 0){
                    $k = "RoleUser";
                }else{
                    $k = "LostDay" . $i;
                }
                $l = str_replace("#day#",$i,$label);
                $chartData[$l]["Level_" . $item['RoleLevel']] = $item[$k];
            }
        }
        return array(
            "data"=> $chartData
        );
    }

    public static function independentKPIHeader($params){
        $header = array(
//            'RecDate'   => array('text' => PubFunc::t('日期')),
//            'SID'   => array('text' => PubFunc::t('服')),
            'RoleLevel'      => array('text' => PubFunc::t('等级'),'rule'=>'numeric'),
//            'RoleUser'      => array('text' => PubFunc::t('累计用户数'),'rule'=>'numeric'),
//            'RoleUserRate'      => array('text' => PubFunc::t('占比'),'rule'=>'numeric'),
        );
        $kpiDict = array(
            'AllUser'   => PubFunc::t('所有玩家'),
            'NewUserLost'  => PubFunc::t('新用户流失'),
            'PayUser'  => PubFunc::t('充值玩家'),
            'LoginUser'=> PubFunc::t("登录用户")
        );
        foreach($kpiDict as $k => $v){
//            $header[date("Y-m-d",$min)] = array('text' => date("Y-m-d",$min),'rule'=>'numeric');
            $header[$k . "_Rate"] = array('text' => $v,'rule'=>'numeric');
        }
        return $header;
    }


    public static function independentKPIData($params){
        $data = ModLevel::independentKPIData($params);

        $kpiDict = array(
            'AllUser'   => PubFunc::t('所有玩家'),
            'NewUserLost'  => PubFunc::t('新用户流失'),
            'PayUser'  => PubFunc::t('充值玩家'),
            'LoginUser'=> PubFunc::t("登录用户")
        );

        $returnData = array();
        foreach($data as $item){
            if(!isset($returnData["Level" . $item['RoleLevel']])){
                $returnData["Level" . $item['RoleLevel']] = array("RoleLevel" => $item['RoleLevel']);
            }
            $returnData["Level" . $item['RoleLevel']][$item['KPI']] = $item["RoleUser"];
        }

        foreach($kpiDict as $k => $v){
            $total = array();

            $allParams = array(
                "total" => array("Level"=>"0"),
                "sumKey"=>array($k)
            );
            $rateConfig = array(
                $k   =>  array("to" =>  $k . "_Rate",$allParams),
            );
            PubStat::setRate($returnData,$rateConfig,$allParams,$total,true);
        }

        //LibUtil::var_dump($returnData);
        return $returnData;
    }

    public static function independentKPIChart($data,$params){
        $chartData = array();
        if(empty($data)){
            return $chartData;
        }
        $kpiDict = array(
            'AllUser'   => PubFunc::t('所有玩家'),
            'NewUserLost'  => PubFunc::t('新用户流失'),
            'PayUser'  => PubFunc::t('充值玩家'),
            'LoginUser'=> PubFunc::t("登录用户")
        );
        foreach($data as $v){
            foreach($kpiDict as $kk => $vv){
                for($i = 1;$i <= 100;$i++){
                    if(!isset($chartData[$vv][$v["RoleLevel"]])){
                        $chartData[$vv][$v["RoleLevel"]] = 0;
                    }
                }
                //               $chartData[$vv][$v["RoleLevel"]] = $v[$kk];
                $chartData[$kpiDict[$kk]][$v["RoleLevel"]] = $v[$kk];
            }
        }
        return array(
            "data"=> $chartData
        );
    }

    public static function activeHeader(){
        return array(
            'RecDate'           => array('text' => '日期'),
            'ActivityId'        => array('text' => '活动ID'),
            'ActivityName'      => array('text' => '活动名称'),
            'L1' => array('text' => '0-25', 'include' => array(
                'L1_ActProcNum'     => array('text' => '参加人数', 'rule' => 'numeric'),
                'L1_ActCompNum'     => array('text' => '达成人数', 'rule' => 'numeric'),
                'L1_ActCompRate'    => array('text' => '达成率', 'rule' => 'numeric'),
            )),
            'L2' => array('text' => '25-50', 'include' => array(
                'L2_ActProcNum'     => array('text' => '参加人数', 'rule' => 'numeric'),
                'L2_ActCompNum'     => array('text' => '达成人数', 'rule' => 'numeric'),
                'L2_ActCompRate'    => array('text' => '达成率', 'rule' => 'numeric'),
            )),
            'L3' => array('text' => '50-75', 'include' => array(
                'L3_ActProcNum'     => array('text' => '参加人数', 'rule' => 'numeric'),
                'L3_ActCompNum'     => array('text' => '达成人数', 'rule' => 'numeric'),
                'L3_ActCompRate'    => array('text' => '达成率', 'rule' => 'numeric'),
            )),
            'L4' => array('text' => '75-100', 'include' => array(
                'L4_ActProcNum'     => array('text' => '参加人数', 'rule' => 'numeric'),
                'L4_ActCompNum'     => array('text' => '达成人数', 'rule' => 'numeric'),
                'L4_ActCompRate'    => array('text' => '达成率', 'rule' => 'numeric'),
            )),
        );
    }

    public static function activeData($params){
        $data = ModLevel::activeData($params);

        $total = array();
        foreach($data as $k=>&$v){
            $v['RecDate'] = "{$params['time']}";
            for($i=1;$i<=4;$i++){
                $v["L{$i}_ActCompRate"] = number_format($v["L{$i}_ActCompNum"] / $v["L{$i}_ActProcNum"] * 100, 2) . '%';
            }
            if($v['ActivityId'] == 'ALL'){
                $total = $v;
                unset($data[$k]);
            }
        }
        unset($v);

        return array(
            'total' => $total,
            'data'  => $data
        );
    }
} 
